import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:io';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/services.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:rxdart/rxdart.dart';
import 'package:super_tool_server/business/config_manager.dart';
import 'package:super_tool_server/common/utils/log.dart';
import 'package:super_tool_server/model/config_info.dart';
import 'package:super_tool_server/model/log_item_info.dart';
import 'package:super_tool_server/resource/r.dart';

class LogManager {
  List<LogItemInfo> logList = [];
  late ConfigInfo configInfo;
  late BehaviorSubject<List<LogItemInfo>> _subject;

  LogManager._() {
    configInfo = ConfigManager.getInstance().getConfigInfo();
    _subject = BehaviorSubject<List<LogItemInfo>>();
  }

  static LogManager? _instance;

  Stream<List<LogItemInfo>> get stream => _subject.stream;

  static LogManager getInstance() {
    if (_instance == null) {
      _instance = LogManager._();
    }
    return _instance!;
  }

  void dispose() {
    _subject.close();
  }

  List<LogItemInfo> getLogList() {
    return logList;
  }

  void addLog(String log, {Color color = const Color(0xAFFFFFFF)}) {
    if (logList.length >= 200) {
      logList.removeAt(0);
    }
    superLog.info(log);
    logList.add(LogItemInfo()
      ..title = log
      ..color = color);
    _subject.add(logList);
  }

  void clear() {
    logList.clear();
    _subject.add([]);
  }
}
